package com.nit.lab.controller.medical;


import com.alibaba.fastjson.JSONObject;
import com.nit.lab.dto.medical.PatientDTO;
import com.nit.lab.dto.system.TokenDTO;
import com.nit.lab.dto.utils.OpResultDTO;
import com.nit.lab.dto.utils.TableReqDTO;
import com.nit.lab.dto.utils.TableRspDTO;
import com.nit.lab.service.medical.PatientService;
import com.nit.lab.utils.HttpRequestReader;
import com.nit.lab.utils.JWTUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping(value = "/api/patient")
public class PatientController {

    @Resource
    private PatientService PatientService;
    private final Logger logger = LoggerFactory.getLogger(PatientController.class);


    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public TableRspDTO list4Table(@RequestBody TableReqDTO tableReqDTO) {
        TableRspDTO tableRspDTO = new TableRspDTO();
        try {
            tableRspDTO = PatientService.list4Table(tableReqDTO);
        } catch (Exception e) {
            logger.error(e.toString());
        }
        return tableRspDTO;
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public OpResultDTO add(@RequestBody PatientDTO PatientDTO) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            opResult.setMessage(PatientService.add(PatientDTO).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }


    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public OpResultDTO update(@RequestBody PatientDTO PatientDTO) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            System.out.print(PatientDTO.getBedId());
            opResult.setMessage(PatientService.update(PatientDTO).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }


    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public OpResultDTO remove(HttpServletRequest request) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            JSONObject jsonRequest = HttpRequestReader.getJsonObject(request);
            String PatientIds = jsonRequest.getString("patientIds");
            System.out.print(PatientIds);
            opResult.setMessage(PatientService.remove(PatientIds).toString());
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    @RequestMapping(value = "/getPatientById", method = RequestMethod.POST)
    public OpResultDTO getPatientById(HttpServletRequest request) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            JSONObject jsonRequest = HttpRequestReader.getJsonObject(request);
            String PatientId = jsonRequest.getString("PatientId");
            opResult.setMessage("success");
            opResult.setResult(PatientService.getPatientById(Long.parseLong(PatientId)));
        } catch (Exception e) {
            logger.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }
}
